﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Linq;
using System.Threading;
using DevExpress.XtraEditors;
using DevExpress.XtraEditors.Controls;
using DevExpress.XtraGrid.Views.Grid;
using DevExpress.XtraGrid.Views.Base;
using DevExpress.LookAndFeel;
using DevExpress.XtraGrid.Columns;
using BLL;
using DTO;
using DevExpress.XtraEditors.Repository;
using DAL;

namespace CRM.GUI.QLKhachHang
{
    public partial class frmCADList : DevExpress.XtraEditors.XtraForm
    {
        private BLsupervisor sup = new BLsupervisor();
        DataTranferEntity trans = new DataTranferEntity();
        UserService _userFunc = new UserService();

        public frmCADList()
        {
            InitializeComponent();
            grdViewTongQuat.BestFitColumns();
            grdViewTongQuat.OptionsView.ColumnAutoWidth = true ;
            grdViewTongQuat.ScrollStyle = ScrollStyleFlags.LiveHorzScroll | ScrollStyleFlags.LiveVertScroll;
            grdViewTongQuat.HorzScrollVisibility = ScrollVisibility.Always;
            btnDelete.Enabled = false;
            IntialConfig();
            IntialLoad();
        }

        private void IntialConfig()
        {
            colName.UnboundType = DevExpress.Data.UnboundColumnType.String;
            colRegion.UnboundType = DevExpress.Data.UnboundColumnType.String;            
        }

        private void IntialLoad()
        {
            cmbUserList.Properties.DataSource = _userFunc.LoadUserbyRole(3);
            cmbUserList.Properties.DisplayMember = "Account";
            cmbUserList.Properties.ValueMember = "User_ID";
        }

        private void LoadScreen()
        {
            Application.Run(new frmProcess());
        }

        private void btnShowList_Click(object sender, EventArgs e)
        {
                if (cmbUserList.EditValue == null)
                {
                   
                    MessageBox.Show("Chưa chọn loại danh sách cần xem", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
                else
                {
                if (cmbUserList.EditValue != null && cmbUserList.EditValue.ToString() != "")
                {
                    LoadCustomerFunc func = new LoadCustomerFunc();
                    DateTime _startdate = new DateTime();
                    DateTime _enddate = new DateTime();
                    _startdate = (DateTime)startdate.Value;
                    _enddate = (DateTime)enddate.Value;
                    UserEntity user = (UserEntity)cmbUserList.EditValue;
                    int resultcall = SharedData.ResultStatusList.Where(a => a.Result_Code == "S").SingleOrDefault().ResultID;

                    grdTongQuat.DataSource = func.LoadCustomerListCAD(_startdate, _enddate, user.UserID, resultcall);
                    btnDelete.Enabled = true;
                }           
        }
        }

        private void btnDelete_Click(object sender, EventArgs e)
        {
            LoadCustomerFunc func = new LoadCustomerFunc();
            var data = (grdTongQuat.DataSource as DataTable);
         
            foreach (DataRow item in data.Rows)
            {
                if ((bool)item["Selected"] == true)
                {
                    long _cusID = long.Parse(item["Customer_ID"].ToString());
                    func.DeleteCusFromDividingWorkings((_cusID));    
                }
            }

            btnShowList_Click(sender, e);
        }

        private void grdviewCALookup_CustomUnboundColumnData(object sender, CustomColumnDataEventArgs e)
        {
            if (e.IsGetData)
            {
                var tmp = e.Row as UserEntity;
                if(e.Column.Name == "colName")
                {
                    e.Value = tmp.LastName + " " + tmp.FirstName;
                }
                if (e.Column.Name == "colRegion")
                {
                    e.Value = SharedData.RegionList.Where(a => a.Region_Code == tmp.Region_Code).SingleOrDefault().RegionName;
                }
            }
        }

    }
}